package com.avcdata.national.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avcdata.aoscustommade.R;
import com.avcdata.national.activity.NationalOverViewActivity;
import com.avcdata.national.utils.DrawCircle;
import com.avcdata.national.utils.MyUtils;

import java.util.List;

import static com.avcdata.national.utils.MyUtils.dip2px;
import static com.avcdata.national.utils.MyUtils.format0;
import static com.avcdata.national.utils.MyUtils.format2;

/**
 * NewAOSApp
 * com.avcdata.national.adapter
 * Created by lic on 2017/8/18.
 * CSDN http://blog.csdn.net/lucasey
 */

public class CategorySynthesisAdapter extends BaseAdapter {

    private Context context;
    private List<NationalOverViewActivity.CateGroiseOverview> list;

    public CategorySynthesisAdapter(Context context, List<NationalOverViewActivity.CateGroiseOverview> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        viewHolder holder = null;
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_cate_sync, null);
            holder = new viewHolder();
            holder.circle_root = (LinearLayout) convertView.findViewById(R.id.circle);
            holder.item_name = (TextView) convertView.findViewById(R.id.item_name);
            holder.item_icon = (ImageView) convertView.findViewById(R.id.item_icon);
            holder.huanbi_icon = (ImageView) convertView.findViewById(R.id.huanbi_icon);
            holder.tongbi_icon = (ImageView) convertView.findViewById(R.id.tongbi_icon);
            holder.huanbi_num = (TextView) convertView.findViewById(R.id.huanbi_tv);
            holder.tongbi_num = (TextView) convertView.findViewById(R.id.tongbi_tv);
            convertView.setTag(holder);
        }else{
            holder = (viewHolder) convertView.getTag();
        }

        holder.huanbi_icon.setVisibility(View.GONE);
        holder.tongbi_icon.setVisibility(View.GONE);

        //市占
        holder.circle_root.removeAllViews ();
        DrawCircle view1 = new DrawCircle(context,dip2px(context,1f),list.get(position).getMarketShare());
        LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT );
        //通知view组件重绘
        view1.invalidate ();
        holder.circle_root.addView ( view1, params1 );
        //处理环比

        try{
            if(Float.parseFloat(list.get(position).getMarketShareLinkRelativeRatio())>0){
//            holder.huanbi_icon.setBackgroundResource(R.mipmap.data_rise);
                holder.huanbi_num.setTextColor(context.getResources().getColor(R.color.red));
                holder.huanbi_num.setText(MyUtils.format2(Float.parseFloat(list.get(position).getMarketShareLinkRelativeRatio()))+"%");
            }else{
//            holder.huanbi_icon.setBackgroundResource(R.mipmap.data_decline);
                holder.huanbi_num.setTextColor(context.getResources().getColor(R.color.green));
                holder.huanbi_num.setText(MyUtils.format2(Math.abs(Float.parseFloat(list.get(position).getMarketShareLinkRelativeRatio())))+"%");
            }
        }catch (Exception e){
            holder.huanbi_num.setTextColor(context.getResources().getColor(R.color.text_one));
            holder.huanbi_num.setText("--");
        }

        //处理同比

        try{
            if(Float.parseFloat(list.get(position).getMarketShareBasisRatio())>0){
//            holder.tongbi_icon.setBackgroundResource(R.mipmap.data_rise);
                holder.tongbi_num.setTextColor(context.getResources().getColor(R.color.red));
                holder.tongbi_num.setText(MyUtils.format2(Float.parseFloat(list.get(position).getMarketShareBasisRatio()))+"%");
            }else{
//            holder.tongbi_icon.setBackgroundResource(R.mipmap.data_decline);
                holder.tongbi_num.setTextColor(context.getResources().getColor(R.color.green));
                holder.tongbi_num.setText(MyUtils.format2(Math.abs(Float.parseFloat(list.get(position).getMarketShareBasisRatio())))+"%");
            }
        }catch (Exception e){
            holder.tongbi_num.setTextColor(context.getResources().getColor(R.color.text_one));
            holder.tongbi_num.setText("--");
        }

        holder.item_name.setText(list.get(position).getCategory());
        Log.e("??",""+list.get(position).getCategory());
        switch (""+list.get(position).getCategory()){
            case "净化器":
                holder.item_icon.setBackgroundResource(R.mipmap.product_aircleaner);
                break;
            case "电储水热水器":
                holder.item_icon.setBackgroundResource(R.mipmap.product_electricwaterheater);
                break;
            case "净水器":
                holder.item_icon.setBackgroundResource(R.mipmap.product_watercleaner);
                break;
            case "热水器":
                holder.item_icon.setBackgroundResource(R.mipmap.product_gaswaterheater);
                break;
            case "燃气热水器":
                holder.item_icon.setBackgroundResource(R.mipmap.product_gaswaterheater);
                break;
            default:
                holder.item_icon.setBackgroundResource(R.mipmap.product_watercleaner);
                break;
        }

        return convertView;
    }

    private class viewHolder{

        private LinearLayout circle_root;

        private TextView tongbi_num;
        private TextView huanbi_num;

        private ImageView tongbi_icon;
        private ImageView huanbi_icon;

        private TextView item_name;
        private ImageView item_icon;

    }

}
